//
//  MainTabBar.swift
//  LBWeibo
//
//  Created by 李博 on 15/6/23.
//  Copyright © 2015年 libo. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    //按钮总数
    private let buttonCount = 5
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = bounds.width / CGFloat(buttonCount)
        let rect = CGRectMake(0, 0, w, bounds.height)
        
        var index = 0
        for v in subviews {
            //判断控件，同时排出UIButton
            if v is UIControl && !(v is UIButton) {
                
                v.frame = CGRectOffset(rect, CGFloat(index) * w, 0)
                index += index == 1 ? 2 : 1
                
            }
        }
        //设置加号按钮的位置
        compoesButton.frame = CGRectOffset(rect, 2 * w, 0)
    }
    
    
    ///懒加载compoesButton
    lazy var compoesButton: UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)

        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)

        //self. 闭包，提前准备好的代码，在需要的时候执行
        self.addSubview(btn)
        
        return btn;
        
    }()
    
    
}
